using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Hematology.Data
{
	[DataObject]
	[Serializable]
	public partial class FaqItem
	{
		#region Constants

		private const string TableName = "[dbo].[FAQItem]";

		#endregion

		#region Properties

		public int? FaqItemId { get; set; }
		public string Question { get; set; }
		public string Answer { get; set; }
		public int? OrderNumber { get; set; }
		public bool? IsDeleted { get; set; }
		public DateTime? CreateDate { get; set; }
		public DateTime? LastModifiedDate { get; set; }

		#endregion

		#region Methods

		#region Mapping Methods

		protected void MapTo(DataSet ds)
		{
			if (ds == null)
				ds = new DataSet();

			if (ds.Tables[TableName] == null)
				ds.Tables.Add(TableName);

			ds.Tables[TableName].Columns.Add("FaqItemId", typeof(Int32));
			ds.Tables[TableName].Columns.Add("Question", typeof(String));
			ds.Tables[TableName].Columns.Add("Answer", typeof(String));
			ds.Tables[TableName].Columns.Add("OrderNumber", typeof(Int32));
			ds.Tables[TableName].Columns.Add("IsDeleted", typeof(Boolean));
			ds.Tables[TableName].Columns.Add("CreateDate", typeof(DateTime));
			ds.Tables[TableName].Columns.Add("LastModifiedDate", typeof(DateTime));

			DataRow dr = ds.Tables[TableName].NewRow();

			dr["FaqItemId"] = FaqItemId ?? (object) DBNull.Value;
			dr["Question"] = Question ?? (object) DBNull.Value;
			dr["Answer"] = Answer ?? (object) DBNull.Value;
			dr["OrderNumber"] = OrderNumber ?? (object) DBNull.Value;
			dr["IsDeleted"] = IsDeleted ?? (object) DBNull.Value;
			dr["CreateDate"] = CreateDate ?? (object) DBNull.Value;
			dr["LastModifiedDate"] = LastModifiedDate ?? (object) DBNull.Value;

			ds.Tables[TableName].Rows.Add(dr);
		}

		protected void MapFrom(DataRow dr)
		{
			FaqItemId = dr["FaqItemId"] != DBNull.Value ? Convert.ToInt32(dr["FaqItemId"]) : FaqItemId = null;
			Question = dr["Question"] != DBNull.Value ? Convert.ToString(dr["Question"]) : Question = null;
			Answer = dr["Answer"] != DBNull.Value ? Convert.ToString(dr["Answer"]) : Answer = null;
			OrderNumber = dr["OrderNumber"] != DBNull.Value ? Convert.ToInt32(dr["OrderNumber"]) : OrderNumber = null;
			IsDeleted = dr["IsDeleted"] != DBNull.Value ? Convert.ToBoolean(dr["IsDeleted"]) : IsDeleted = null;
			CreateDate = dr["CreateDate"] != DBNull.Value ? Convert.ToDateTime(dr["CreateDate"]) : CreateDate = null;
			LastModifiedDate = dr["LastModifiedDate"] != DBNull.Value ? Convert.ToDateTime(dr["LastModifiedDate"]) : LastModifiedDate = null;
		}

		public static List<FaqItem> MapFrom(DataSet ds)
		{
			// Initialise Collection.
			List<FaqItem> objects = new List<FaqItem>();

			// Validation.
			if (ds == null)
				throw new ApplicationException("Cannot map to dataset null.");
			
			if (ds.Tables[TableName].Rows.Count == 0)
				return objects;

			if (ds.Tables[TableName] == null)
				throw new ApplicationException("Cannot find table [dbo].[FAQItem] in DataSet.");

			if (ds.Tables[TableName].Rows.Count < 1)
				throw new ApplicationException("Table [dbo].[FAQItem] is empty.");

			// Map DataSet to Instance.
			foreach (DataRow dr in ds.Tables[TableName].Rows)
			{
				FaqItem instance = new FaqItem();
				instance.MapFrom(dr);
				objects.Add(instance);
			}

			// Return collection.
			return objects;
		}

		#endregion

		#region CRUD Methods

		public static FaqItem Get(Int32 faqItemId)
		{
			FaqItem instance = new FaqItem();

			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_SELECT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, faqItemId))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					if (ds == null || ds.Tables[0].Rows.Count == 0) throw new ApplicationException(String.Format("Could not get FAQItem ID:{0} from Database.", faqItemId));
					ds.Tables[0].TableName = TableName;

					instance.MapFrom(ds.Tables[0].Rows[0]);
				}
			}
			return instance;
		}

		#region INSERT

		private void Insert(String question, String answer, Int32? orderNumber, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction = null)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_INSERT";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, question, answer, orderNumber, isDeleted, createDate, lastModifiedDate))
			{
				FaqItemId = Convert.ToInt32(transaction == null ? db.ExecuteScalar(dbCommand) : db.ExecuteScalar(dbCommand, transaction));
			}
		}

		/// <summary>
		/// Insert current FAQItem to database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Insert(DbTransaction transaction = null)
		{
			Insert(Question, Answer, OrderNumber, IsDeleted, CreateDate, LastModifiedDate, transaction);
		}

		#endregion

		#region UPDATE

		private static void Update(Int32? faqItemId, String question, String answer, Int32? orderNumber, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@faqItemId"].Value = faqItemId;
				dbCommand.Parameters["@question"].Value = question;
				dbCommand.Parameters["@answer"].Value = answer;
				dbCommand.Parameters["@orderNumber"].Value = orderNumber;
				dbCommand.Parameters["@isDeleted"].Value = isDeleted;
				dbCommand.Parameters["@createDate"].Value = createDate;
				dbCommand.Parameters["@lastModifiedDate"].Value = lastModifiedDate;

				if (transaction == null)
					db.ExecuteNonQuery(dbCommand);
				else
					db.ExecuteNonQuery(dbCommand, transaction);
			}
		}

		private static void Update(Int32? faqItemId, String question, String answer, Int32? orderNumber, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Update(faqItemId, question, answer, orderNumber, isDeleted, createDate, lastModifiedDate, null);
		}

		private static void Update(FaqItem fAQItem)
		{
			fAQItem.Update();
		}

		private static void Update(FaqItem fAQItem, DbTransaction transaction)
		{
			fAQItem.Update(transaction);
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		private void Update(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_UPDATE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand))
			{
				db.DiscoverParameters(dbCommand);

				dbCommand.Parameters["@faqItemId"].SourceColumn = "FaqItemId";
				dbCommand.Parameters["@question"].SourceColumn = "Question";
				dbCommand.Parameters["@answer"].SourceColumn = "Answer";
				dbCommand.Parameters["@orderNumber"].SourceColumn = "OrderNumber";
				dbCommand.Parameters["@isDeleted"].SourceColumn = "IsDeleted";
				dbCommand.Parameters["@createDate"].SourceColumn = "CreateDate";
				dbCommand.Parameters["@lastModifiedDate"].SourceColumn = "LastModifiedDate";

				using (DataSet ds = new DataSet())
				{
					MapTo(ds);
					ds.AcceptChanges();
					ds.Tables[0].Rows[0].SetModified();

					if (transaction == null)
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, UpdateBehavior.Standard);
					else
						db.UpdateDataSet(ds, TableName, null, dbCommand, null, transaction);
				}
			}
		}

		/// <summary>
		/// Updates changes to the database.
		/// </summary>
		private void Update()
		{
			Update((DbTransaction)null);
		}
		#endregion

		#region DELETE

		public static void Delete(Int32? faqItemId, DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, faqItemId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
		}

		public static void Delete(Int32? faqItemId)
		{
			Delete(
				faqItemId);
		}

		/// <summary>
		/// Delete current FAQItem from database.
		/// </summary>
		/// <param name="transaction">optional SQL Transaction</param>
		public void Delete(DbTransaction transaction)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_DELETE";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, FaqItemId))
			{
				if (transaction != null)
				{
					db.ExecuteNonQuery(dbCommand, transaction);
				}
				else
				{
					db.ExecuteNonQuery(dbCommand);
				}
			}
			FaqItemId = null;
		}

		/// <summary>
		/// Delete current FAQItem from database.
		/// </summary>
		public void Delete()
		{
			Delete((DbTransaction)null);
		}

		#endregion

		#region SEARCH

		private static List<FaqItem> Search(Int32? faqItemId, String question, String answer, Int32? orderNumber, Boolean? isDeleted, DateTime? createDate, DateTime? lastModifiedDate)
		{
			Database db = DatabaseFactory.CreateDatabase();
			const string sqlCommand = "[dbo].gspFAQItem_SEARCH";
			using (DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand, faqItemId, question, answer, orderNumber, isDeleted, createDate, lastModifiedDate))
			{
				using (DataSet ds = db.ExecuteDataSet(dbCommand))
				{
					ds.Tables[0].TableName = TableName;
					return MapFrom(ds);
				}
			}
		}

		private static List<FaqItem> Search(FaqItem searchObject)
		{
			return Search(searchObject.FaqItemId, searchObject.Question, searchObject.Answer, searchObject.OrderNumber, searchObject.IsDeleted, searchObject.CreateDate, searchObject.LastModifiedDate);
		}

		#endregion

		#endregion

		#endregion
	}
}